package jason.jeditplugin;

import jason.mas2j.MAS2JProject;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class PluginUtils {
	/**
	 * Gets line number from ParseException message of Jason.
	 */
	public static int getLineNumberFromMsg(String msg) {
		try {
			String[] msgTokens = msg.split(" ");
			for(int i = 0; i < msgTokens.length; i++) {
				if (msgTokens[i].equals("line")) {
					int line = Integer.parseInt(msgTokens[i + 1].substring(0, msgTokens[i + 1].length() - 1));
					//System.out.println("Linha: "+line);
					return line;
				}
			}
			
			Matcher matcher = Pattern.compile("\\.(" + jason.mas2j.MAS2JProject.AS_EXT + "|" + jason.mas2j.MAS2JProject.ASM_EXT + "):(\\d+)\\D", Pattern.CASE_INSENSITIVE).matcher(msg);
			if (matcher.find()) {
				return Integer.parseInt(matcher.group(2));
			}
		} catch (NumberFormatException e) {
		}
		return 0;
	}

	/**
	 * Gets position of start char in the document contents.
	 * 
	 * @param documentContents
	 *            Got by editor input.
	 */
	public static int getCharStart(String documentContents, int line, String msg) {
		if(msg.contains("in module \"")) return -1;
		
		String[] msgTokens = msg.split(" ");
		for(int i = 0; i < msgTokens.length; i++) {
			if (msgTokens[i].equals("column")) {
				int col = Integer.parseInt(msgTokens[i + 1].substring(0, msgTokens[i + 1].length() - 6));
				return (col - 1); // starts by 0
			}
		}
		
		return -1;
	}
	
	/**
	 * Gets position of end char in the document contents.
	 * 
	 * @param documentContents
	 *            Got by editor input.
	 */
	public static int getCharEnd(String documentContents, int line, int charStart, String msg) {
		String[] lines = documentContents.split("\n");
		String str = lines[line - 1].substring(charStart);
		String[] strTokens = str.split("[^.:\"a-zA-Z0-9]", 2);
		return charStart + Math.max(1, strTokens[0].length());
	}

	/**
	 * Gets position of start char in the document contents.
	 * 
	 * @param documentContents
	 *            Got by editor input.
	 */
	public static int getCharStartFromLexicalError(String documentContents, int line, String msg) {
		if(msg.contains("in module \"")) return -1;
		
		String[] msgTokens = msg.split(" ");
		for(int i = 0; i < msgTokens.length; i++) {
			if (msgTokens[i].equals("column")) {
				int col = Integer.parseInt(msgTokens[i + 1].substring(0, msgTokens[i + 1].length() - 1));
				return (col - 1); // starts by 0
			}
		}
		
		return 0;
	}

	/**
	 * Gets position of end char in the document contents.
	 * 
	 * @param documentContents
	 *            Got by editor input.
	 */
	public static int getCharEndLexicalError(String documentContents, int line, int charStart, String msg) {
		String[] msgTokens = msg.split(" ");
		for(int i = 0; i < msgTokens.length; i++) {
			if (msgTokens[i].equals("Encountered:")) {
				return charStart + (msgTokens[i + 1].length() - 2);
			}
		}
		
		return 0;
	}
}
